from sklearn.datasets import load_iris
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform, randint
from sklearn.neural_network import MLPClassifier

# 加载数据集
iris = load_iris()
X, y = iris.data, iris.target

# 定义参数分布
param_dist = {
    'hidden_layer_sizes': randint(1, 501),
    'activation': ['tanh', 'relu'],
    'alpha': uniform(loc=0.0001, scale=0.01),
    'learning_rate_init': uniform(loc=0.0001, scale=0.01)
}

# 创建MLPClassifier实例
mlp = MLPClassifier(max_iter=1000)

# 创建RandomizedSearchCV实例
random_search = RandomizedSearchCV(estimator=mlp, param_distributions=param_dist, n_iter=20, cv=5, scoring='accuracy')

# 执行搜索
random_search.fit(X, y)

# 输出最佳参数和最佳分数
print("Best parameters: ", random_search.best_params_)
print("Best score: ", random_search.best_score_)
